package com.easydev.basic.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *  spring bean工具类，用来从容器中获取bean实例
 * 
 * @author gulingfeng<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2016年3月1日 <br>
 */
public class BeanFactoryUtils implements ApplicationContextAware {

    /**
     * spring容器上下文
     */
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanFactoryUtils.applicationContext = applicationContext;
    }

    /**
     * 通过类型获取容器中的bean
     * 
     * @author glf
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    /**
     * 通过class获取容器中的bean
     * 
     * @author glf
     * @param requiredType
     * @return
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * 通过名称+类型获取容器中的bean
     * 
     * @author glf
     * @param name
     * @param requiredType
     * @return
     */
    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

}
